package com.framework.loippi.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * 路径工具类
 * 
 * @author wangcj
 * 
 */
public class PathUtil {

	/**
	 * 获取Tomcat的webapps下目录的ROOT目录
	 */
	public static String getSavePicturePath() {
		String strResult = "";
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		
		StringBuffer strBuf = new StringBuffer();
		String projectPath = PublicUtil.getPorjectPath().replaceAll("\\\\",
				"/");
		projectPath = splitString(projectPath, "bin/");

		strBuf.append(projectPath);
		strBuf.append("webapps/ROOT/");

		strResult = strBuf.toString();

		return strResult;
	}

	private static String splitString(String str, String param) {
		String result = str;

		if (str.contains(param)) {
			int start = str.indexOf(param);
			result = str.substring(0, start);
		}

		return result;
	}
	
	/*
	 * 获取classpath1
	 */
	@Deprecated
	public static String getClasspath(){
		String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../").replaceAll("file:/", "").replaceAll("%20", " ").trim();	
		if(path.indexOf(":") != 1){
			path = File.separator + path;
		}
		return path;
	}
	
	/*
	 * 获取classpath2
	 */
	@Deprecated
	public static String getClassResources(){
		String path =  (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))).replaceAll("file:/", "").replaceAll("%20", " ").trim();	
		if(path.indexOf(":") != 1){
			path = File.separator + path;
		}
		return path;
	}
	
	@Deprecated
	public static String getWebAppPathAddr() {
		String strResult = "";

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();

		StringBuffer strBuf = new StringBuffer();

		strBuf.append(request.getScheme() + "://");
		strBuf.append(request.getServerName() + ":");
		strBuf.append(request.getServerPort() + "/");

		//strBuf.append(request.getContextPath() + "/");

		strResult = strBuf.toString();// +"ss/";//加入项目的名称

		return strResult;
	}
	/**
	 * 获取Tomcat的webapps目录
	 */
	public static String getWebAppsRealPath() {
		
		String filePath = PathUtil.class.getResource("").getPath()
				.replaceAll("%20", " ").replaceAll("\\\\","/");
		StringBuffer strBuf = new StringBuffer();
		String webappsPath = splitString(filePath, "webapps/");
		strBuf.append(webappsPath);
		strBuf.append("webapps/");

		webappsPath = strBuf.toString();

		return webappsPath;
	}
	
	/** 
     * 获取服务器端的webapps路径 
     * @return 
     */  
	@Deprecated
    public static String tomcatWebApps(){ 
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
    	String tomcatRoot = request.getSession().getServletContext().getRealPath("/");  
        String[] foo = tomcatRoot.split("/");  
        StringBuilder tomcatWebAppsBuilder = new StringBuilder();  
        int i = 0;  
        for(String paths : foo){  
            ++i;  
            if(i != foo.length){  
                tomcatWebAppsBuilder.append(paths);  
                tomcatWebAppsBuilder.append("/");  
            }  
        }  
        String tomcatWebApps = tomcatWebAppsBuilder.toString();
        return tomcatWebApps;
    }  
	
	
}
